package com.htt.app.cuasotinhyeu.utils;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;

public class Device {

	public static String getDeviceID(Context ctx) {
		TelephonyManager tm = (TelephonyManager) ctx.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getDeviceId();
	}
	
	
	public static String getSerialNum(Context ctx) {
		TelephonyManager tm = (TelephonyManager) ctx.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getSimSerialNumber();
	}
	
	
	public static String getAndroidId(Context ctx) {
		return Secure.getString(ctx.getContentResolver(),Secure.ANDROID_ID);
	}
	
	public static int getVersionCode(Context ctx) {
		int versionCode = 0;
		try {
			versionCode = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0).versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
		}
		return versionCode;
	}
	
	public static String getVersionName(Context ctx) {
		String versionName = "";
		try {
			versionName = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
		}
		return versionName;
	}
	
	public static String getEmail(Context context) {
		    AccountManager accountManager = AccountManager.get(context); 
		    Account account = getAccount(accountManager);
		    
		    if (account == null) {
		      return null;
		    } else {
		      return account.name;
		    }
		  }
	
	private static Account getAccount(AccountManager accountManager) {
		    Account[] accounts = accountManager.getAccountsByType("com.google");
		    Account account;
		    if (accounts.length > 0) {
		      account = accounts[0];      
		    } else {
		      account = null;
		    }
		    return account;
		 }
	
	
}
